package com.frxuexi.shop.view

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.frxuexi.adapter.OrderAdapter
import com.frxuexi.lib_base.alipay.MyPay
import com.frxuexi.lib_base.entity.OrderBean
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.RequestType
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.shop.R
import com.frxuexi.shop.databinding.ActivityOrderBinding
import com.frxuexi.shop.intent.CartIntent
import com.frxuexi.shop.intent.OrderIntent
import com.frxuexi.shop.viewmodel.OrderViewModel
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

@Route(path = "/shop/OrderActivity")
@AndroidEntryPoint
class OrderActivity : BaseActivity<ActivityOrderBinding,OrderIntent,OrderViewModel>() {
    override val binding: ActivityOrderBinding by lazy { ActivityOrderBinding.inflate(layoutInflater) }
    override val vm: OrderViewModel by viewModels<OrderViewModel>()
    val orderAdapter by lazy { OrderAdapter() }
    var orderId=""
    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        when(it.type){
                            RequestType.DEFAULT->{
                                val orderBean = it.data as OrderBean
                                orderAdapter.apply {
                                    data.clear()
                                    addData(orderBean.list)
                                    notifyDataSetChanged()
                                }
                            }
                            RequestType.PAY->{
                                vm.sendIntent(OrderIntent.getMyOrder("0",20,1))
                            }

                            else -> {}
                        }
                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        EventBus.getDefault().register(this)
        binding.orderTab.apply {
            addTab(newTab().setText("未支付"),true)
            addTab(newTab().setText("待发货"),false)
            addTab(newTab().setText("待收获"),false)
            addTab(newTab().setText("待评价"),false)
            addTab(newTab().setText("已完成"),false)
            addTab(newTab().setText("售后/退款"),false)
            addOnTabSelectedListener(object :OnTabSelectedListener{
                override fun onTabSelected(tab: TabLayout.Tab?) {
                    val type = if(tab?.position==5) "-3" else tab?.position.toString()
                    vm.sendIntent(OrderIntent.getMyOrder(type,20,1))
                }

                override fun onTabUnselected(tab: TabLayout.Tab?) {
                }

                override fun onTabReselected(tab: TabLayout.Tab?) {
                }

            })
        }


        binding.orderRv.apply {
            adapter=orderAdapter
            layoutManager=LinearLayoutManager(this@OrderActivity)
        }

        orderAdapter.addChildClickViewIds(R.id.btn_order)
        orderAdapter.setOnItemChildClickListener { adapter, view, position ->
            val payInfo = orderAdapter.data.get(position)
            when(payInfo.orderStatus){
                "待支付"->{
                    orderId=payInfo.orderId
                    MyPay.payV2(payInfo.payPrice.toString(),this)
                }
                "待发货"->{

                }

            }
        }

        vm.sendIntent(OrderIntent.getMyOrder("0",20,1))

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun getPayResult(result:String){
        Log.i("lr",result)
        if(result=="payOk"){
            val map = hashMapOf<String, Any>(
                "from" to "",
                "orderNo" to orderId,
                "payChannel" to "alipay",
                "payType" to "yue",
                "scene" to 0,
                "uni" to ""
            )
            vm.sendIntent(OrderIntent.payOrder(map))
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
}